﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace Transformer.iHunter.UI
{
    public partial class FilterProviderList : UserControl
    {
        private LocalClient client = null;

        public FilterProviderList()
        {
            InitializeComponent();
        }

        public LocalClient Client
        {
            get { return client; }
            set
            {
                client = value;
                RefreshList();
            }
        }

        protected void RefreshList()
        {
            if (client == null)
            {
                this.listView1.Items.Clear();
                return;
            }

            this.listView1.BeginUpdate();
            this.listView1.Items.Clear();

            foreach (IFilterProvider provider in client.FilterProvidersDictionary.Values)
            {
                if (provider.Guid == Guid.Empty)
                {
                    continue;
                }

                ListViewItem item = this.listView1.Items.Add(provider.Name);
                item.SubItems.Add(provider.Description);
                item.Tag = provider;
            }

            this.listView1.EndUpdate();
        }

        /// <summary>
        /// Gets or sets the selected provider GUID.
        /// </summary>
        public Guid SelectedProviderGuid
        {
            get
            {
                var items = this.listView1.SelectedItems;
                if (items.Count == 0)
                {
                    return Guid.Empty;
                }

                IFilterProvider provider = items[0].Tag as IFilterProvider;
                if (provider == null)
                {
                    return Guid.Empty;
                }

                return provider.Guid;
            }
        }

        /// <summary>
        /// Gets the selected Filter provider.
        /// </summary>
        public IFilterProvider SelectedProvider
        {
            get
            {
                Guid guid = SelectedProviderGuid;
                return (guid == Guid.Empty)
                    ? null
                    : client.FilterProvidersDictionary[guid];
            }
        }
    }
}
